#include <QtGui/QApplication>
#include <QDebug>
#include <QDirIterator>
#include <QStringList>

#include "leaqmainwindow.h"
#include "leaqpluginmanager.h"
#include "leaqextensionterface.hpp"
#include "leaqmoduleinterface.hpp"

#include "leaqversion.hpp"

QStringList modulesPaths(QString path)
{
    qDebug() << Q_FUNC_INFO;
    qDebug() << "path:" << path;

    QStringList modulePathCollection;

    QDirIterator dirIterator(path, QDirIterator::Subdirectories);

    while(dirIterator.hasNext()) {
        modulePathCollection.append(dirIterator.next());
    }

    return modulePathCollection;
}

int main(int argc, char* argv[]) {

    QApplication app(argc, argv);

    qDebug() << "--- show splash ---";
    qDebug() << "leaq version:" << leaq::kernel::version::get();

    qDebug() << "--- loading extensions ---";

    leaq::kernel::PluginManager<leaq::kernel::IExtension> extensionManager;
    extensionManager.load(
                QApplication::applicationDirPath() + "/extensions");

    qDebug() << "--- staring extensions ---";

    extensionManager.start();

    qDebug() << "--- loading modules ---";

    QString modulesDir = QApplication::applicationDirPath() + "/modules";

    leaq::kernel::PluginManager<leaq::kernel::IModule> moduleManager;
    moduleManager.load(modulesPaths(modulesDir));

    qDebug() << "--- starting modules ---";

    moduleManager.start();

    qDebug() << "--- show main window ---";

    LeaqMainWindow mainWindow;
    mainWindow.show();

    int retCode = app.exec();

    qDebug() << "--- shutting down ---";

    return retCode;
}
